﻿/*
    This file is part of File Renamer.

    File Renamer is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 3.

    File Renamer is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace FileRenamer
{
    class FileLoader
    {
        public FileInfo[] loadedFiles;
        public string[] ExtractedFileExtensions;

        public FileLoader()
        {
            loadedFiles = new FileInfo[0];

        }

        private void ExtractLoadedFileExtensions()
        {
            List<string> Extensions = new List<string>();

            for (int i = 0; i < loadedFiles.Length; i++)
            {
                if (!Extensions.Contains(loadedFiles[i].Extension))
                    Extensions.Add(loadedFiles[i].Extension);
            }

            ExtractedFileExtensions = Extensions.ToArray();
        }

        public FileInfo[] LoadFiles(string SelectedPath)
        {
            DirectoryInfo dir = new DirectoryInfo(SelectedPath);
            loadedFiles = dir.GetFiles();
            ExtractLoadedFileExtensions();
            return loadedFiles;
        }

        public FileInfo[] LoadFilesFiltered(string SelectedPath, bool filterByExtensions, string[] extensions,
            bool filterByOlderThan, bool filterByNewerThan,
            DateTime OlderThan, DateTime NewerThan)
        {
            LoadFiles(SelectedPath);//First loads all files and then filters

            List<FileInfo> filteredFiles = new List<FileInfo>();
            List<string> localExtensions = new List<string>();

            //Creating extensions list
            for (int i = 0; i < extensions.Length; i++)
            {
                localExtensions.Add(extensions[i]);
            }
            
            if (extensions.Length > 0 && filterByExtensions)//Must execute extension filter
            {
                //Running throught all loaded files and filtering
                for (int i = 0; i < loadedFiles.Length; i++)
                {
                    //Extension filter
                    if (localExtensions.Contains(loadedFiles[i].Extension))
                    {
                        if (filterByNewerThan && filterByOlderThan)
                        {
                            if (loadedFiles[i].CreationTime > NewerThan
                                && loadedFiles[i].CreationTime < OlderThan)
                            {
                                filteredFiles.Add(loadedFiles[i]);
                            }
                        }
                        else if (filterByNewerThan)
                        {
                            if (loadedFiles[i].CreationTime > NewerThan)
                            {
                                filteredFiles.Add(loadedFiles[i]);
                            }
                        }
                        else if (filterByOlderThan)
                        {
                            if (loadedFiles[i].CreationTime < OlderThan)
                            {
                                filteredFiles.Add(loadedFiles[i]);
                            }
                        }
                        else
                        {
                            filteredFiles.Add(loadedFiles[i]);
                        }
                    }
                }                
            }
            else//Extension filter is not required
            {
                //Running throught all loaded files and filtering
                for (int i = 0; i < loadedFiles.Length; i++)
                {
                    if (filterByNewerThan && filterByOlderThan)
                    {
                        if (loadedFiles[i].CreationTime > NewerThan
                            && loadedFiles[i].CreationTime < OlderThan)
                        {
                            filteredFiles.Add(loadedFiles[i]);
                        }
                    }
                    else if (filterByNewerThan)
                    {
                        if (loadedFiles[i].CreationTime > NewerThan)
                        {
                            filteredFiles.Add(loadedFiles[i]);
                        }
                    }
                    else if (filterByOlderThan)
                    {
                        if (loadedFiles[i].CreationTime < OlderThan)
                        {
                            filteredFiles.Add(loadedFiles[i]);
                        }
                    }
                    else
                    {
                        filteredFiles.Add(loadedFiles[i]);
                    }
                } 
            }

            loadedFiles = filteredFiles.ToArray();

            return loadedFiles;
        }

        public void DeleteFiles()
        {
            for (int i = 0; i < loadedFiles.Length; i++)
            {
                loadedFiles[i].Delete();
            }
        }
    }
}
